package cn.usain.service.impl;
import cn.usain.mapper.ProductCollectionMapper;
import cn.usain.mapper.SpecialsMapper;
import cn.usain.service.ProductCollectionService;
import com.jiyun.common.pojo.home.Special;
import com.jiyun.common.pojo.product.ProductCollection;
import com.jiyun.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductCollectionServiceImpl implements ProductCollectionService {
    @Autowired
    private ProductCollectionMapper productCollectionMapper;


    @Autowired
    public SpecialsMapper specialsMapper;

    @Override
    public boolean addProductToCollection(Integer userId, Integer productId) {
        try {
            int result = productCollectionMapper.insertProductCollection(userId, productId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Special> getCollectedProductsByUserId(Integer userId) {
        List<Integer> collectedProductIds = specialsMapper.getCollectedProductIdsByUserId
                (userId);
        List<Special> collectedProducts = new ArrayList<>();
        for (Integer productId : collectedProductIds) {
            Special special = specialsMapper.getProductById(productId);
            if (special!= null) {
                collectedProducts.add(special);
            }
        }
        return collectedProducts;
    }

    @Override
    public R<List<ProductCollection>> findCollectList(Integer userId) {
//        List<ProductCollection> productCollectionList =  productCollectionMapper.findCollectList(userId);
//        for (ProductCollection productCollection : productCollectionList) {
//            System.out.println(productCollection);
//        }
//        List<Integer> list = productCollectionList.stream().map(ProductCollection::getProductId).toList();
//        List<Special> productList = productFeign.findProductList(list);
//        return R.suceess(productList);
        return productCollectionMapper.findCollectList(userId);
    }
}
